package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016102500759625";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVrOtQw8T38phRrdJ7qdOsQxikdB7xi6FKmoGcxXwJLiJ9cEtnjUtCssI6WxZuqzdookDP9sJzK2wyyLmX2sDLE/n43tBIE8MItJ379O7PNBwDpmb8e8wCEogD13UVGouVaPR8qrUAeeQi7V2v8Svc5jU1K+411IpDc4YTG9aK+AZs1TgM85uPYz34FMZbLdzgFD9VvHuS7Gpah6hjvAy55c8EjoptoPfs65AzX6L/KOQfgN05nRvRGIlFiqCahvNg2FhjF0wQxF/a3Zmvnf4vL627JjFZpmbGgLDRww9Hwoh++1EzTpZDtafC4CllCxSYbK/nxAYObb4DeG1JafMvAgMBAAECggEAYAFjixGXpQcZxsJ9EU9Uc9kx1EKbmonaslS6sIlHz0elXdqKs2hBDw5EYVQN5nyydM4mTn7UKTWAGG8CBLKPGXO90nFxHdp3Ln1UHMn16Dhfg8PXqTnrGKU0U2p/MkMtp2+D6vdAVIba28BvaKyEWxPaqzl7eVHUQgG2zUj863PCBpkathg8Oj/3QNkaPNRWpNX2DSntuoUpz4aIZl+x/fGwCLLbnmOQExMQpcoh/S78dqxO+Qq8NNTDhasC/39sqQfEl12j83PWvL0RTVHJmg7ZTdNeVKm/tdR704YQO6G08HJHyexAWxfgdLHCcE/WgmNifRagFXqUV/qrVJjaMQKBgQDUupH/w90uOadPccMZqRySZeGLLkLbxi2kyqqZ1T9U/PRz52vJ6/xE/VAQyrKmhoqRlHutxwOb3SPI0AG+/WJu5XFOKBEeXh6Hw+E5vCyt4OvJKJplc1JltLdwWN3yPaxkZT5Es/n+UC/oVgvi130ZEb9bv1HxWiPUE+/DdH9/lwKBgQC0HvjcUjRYPJNewAAKEOCUzA3wmf07rfYoOEVQ4cisqzqz1bbhT6HYmRNXjTKyYsxv5ffCY1ikIrIuZZ/r3Ch4CBUBbgy90GqZxML1kIlVJ3nfDQigMUglGYgBzSMJaFJRUrCLfP9uWvav7EO7TS1IUvf1/MZCmM6jOQa/CoYcKQKBgFEGZwmmDp7GsuVdjVQ+x3UzfP7INgsDpur3jpddU+wJFQr4gGO+kK2uVkqvCfDHmgvtJFaCUOuTBUg9ggy+8jkPB2Mfe7W3X6WShDy7YcEwxF4wKu6tSBsZBC20i4Ty3LC4p0j/zB6DaFv0Oj7CmRFtYxeM/acSEEd8+HvX6aAtAoGBAKWxJB6QpERTHQH5P1ADLITUnTIqha/jnGBtOx5nRNHKBgsuWrgzJ9/3GLreUbeVeRiqhNrVxtHedOiHZbBzaurYDHKvSJtH4Pgo72s0FZE26j5MYBX0n7g5H0sA6O0bgTX8ChmW1+YtWWaFwdzH5xLz99PaPos+xD+UEuekh6/ZAoGACoIqNtTa6KEAi5iXMUtK5iEDxXH5drdiE2Ub6YIHNOkDkhJZ+giJHYr4C8jwHRBq5xYVU1jDC0Hxw//+/GPhb294y+uj4Gvc8CfAr1biXzyrNZ5irvh9li7GIhfbCSqdDyBUKf5o6XnASMDvaGht86Fc02aX1cXlog4DRe/vXmU=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmuDw1rRkIrd85Elp3Htvct6AA7jVeo98hCOlulqdYD0ImXK0mwtNdZuKyB3fkAeA3f86goD4NK5wZEz+xfnsouqNqr9JC0F4OUsDaBFGisyhNPjd08W8qTqKGHXa1pLHXqhgiB8MlDYQA0nA/0S5lM9WDGTNAe469C/aCkUCwdJ6FGURj0w2obiQa849JEycq7HrANa+uWM3B3P1dcEa7G+fc4nXlL8otZJPYUVZVcqiBdzAicYXpBMiowPwgVn6nqHK+InSAde6XxVW1RVYTQTVPw9SLDJhHmqf/c/rz9HNurDMGMu6U5aDQSJOOIF4z9kW4IRM7i3iSgGbPBPE+wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://eeee.utools.club/payed/notify";//通过utools内网穿透转到192.168.56.10:80的nginx，nginx再转到指定地址

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
